package com.onetech.adrewards.free.activities;

import java.util.List;

import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.analytics.tracking.android.EasyTracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.adapters.PointHistoryListAdapter;
import com.onetech.adrewards.free.customviews.LoadMoreListView;
import com.onetech.adrewards.free.customviews.LoadMoreListView.OnLoadMoreListener;
import com.onetech.adrewards.free.models.PointHistoryModel;
import com.onetech.adrewards.free.objects.PointHistoryDTO;
import com.todddavies.components.progressbar.ProgressWheel;

public class PointHistoryActivity extends BaseActivity implements OnLoadMoreListener{	
	private View mNotFoundData=null;
	private View mWaitingForLoading=null;
	private View mBtnRetry=null;
	private ProgressWheel mProgress=null;
	private LoadMoreListView mListView;
	private View mNoPointHistoryView = null;
	private TextView mTitleBar=null;
	
	private PointHistoryListAdapter mAdapter;
	private LoadPointHistory mLoadPointHistory=null;
	
	protected int pageNo=1;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setMainView(R.layout.activity_point_history);
		setAppTittle(getString(R.string.point_history_view_title));
		showFooterView(false);
		showMyPoints(false);		
		initView();
		initListener();
	}	
	private void initView(){
		mTitleBar=(TextView)findViewById(R.id.titleBar);		
		mTitleBar.setText(getString(R.string.point_history_view_title_bar)+ String.valueOf(mUserCoins)+"pt");
		
		mListView = (LoadMoreListView) findViewById(R.id.myPointHistory);
		mListView.setVisibility(View.GONE);
		mAdapter = new PointHistoryListAdapter(this);
		mListView.setAdapter(mAdapter);
		
		mNoPointHistoryView = (LinearLayout)findViewById(R.id.no_history);
		showNoHistory(false);
		initNotFoundAndLoadingView();
	}
	private void updateTitleBar(){
		mTitleBar.setText(getString(R.string.point_history_view_title_bar)+ String.valueOf(mUserCoins)+"pt");
	}
	private void initListener(){
		initRetryButtonListener();
	}
	private void showNoHistory(boolean isShowNoHistory) {
		mNoPointHistoryView.setVisibility(isShowNoHistory ? View.VISIBLE
				: View.GONE);
	}

	protected void initNotFoundAndLoadingView(){
		mNotFoundData=(LinearLayout)findViewById(R.id.notFoundDataContainer);		
		mWaitingForLoading=(LinearLayout)findViewById(R.id.waitingLoading);
		mProgress=(ProgressWheel)findViewById(R.id.progressWheel);
		mProgress.spin();
		showNotFoundOrLoadingView(false, true);
		mBtnRetry=(LinearLayout)findViewById(R.id.btnRetry);
	}
	protected void initRetryButtonListener(){
		mBtnRetry.setOnClickListener(this);
	}
	protected void showNotFoundOrLoadingView(boolean isShowNotFound,boolean isShowLoading){
		if(isShowNotFound){
			mNotFoundData.setVisibility(View.VISIBLE);
		}else{
			mNotFoundData.setVisibility(View.GONE);
		}
		if(isShowLoading){
			mWaitingForLoading.setVisibility(View.VISIBLE);
		}else{
			mWaitingForLoading.setVisibility(View.GONE);
		}
	}
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnRetry:
			pageNo = 1;
			mListView.setVisibility(View.VISIBLE);
			showNotFoundOrLoadingView(false, true);
			loadPointHistory();
			break;
			default:
				super.onClick(v);
				break;
		}
	}
	@Override
	protected void onResume() {
		super.onResume();
		if (mAdapter != null && mAdapter.getCount() < 1) {
			loadPointHistory();
		}
		updateTitleBar();
	}
	private void loadPointHistory() {
		if(mLoadPointHistory==null || mLoadPointHistory.getStatus()==Status.FINISHED){
			mLoadPointHistory=new LoadPointHistory();
			mLoadPointHistory.execute();
		}
	}
	private class LoadPointHistory extends AsyncTask<Void, Void, List<PointHistoryDTO>> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			showNotFoundOrLoadingView(false, true);
			showNoHistory(false);
		}

		@Override
		protected List<PointHistoryDTO> doInBackground(Void... params) {
			try {
				return PointHistoryModel.getInstance().getPointHistory(String.valueOf(pageNo));
			} catch (Exception e) {
				return null;
			}
		}
		
		@Override
		protected void onPostExecute(List<PointHistoryDTO> result) {
			super.onPostExecute(result);
			try {
				if (result != null) {
					if (result.size() < 1) {
						if (mAdapter.getCount() < 1) {
							showNotFoundOrLoadingView(false, false);
							showNoHistory(true);
							mListView.setVisibility(View.GONE);
						} else {
							showNoHistory(false);
							showNotFoundOrLoadingView(false, false);
							mListView.setVisibility(View.VISIBLE);
						}						
					} else {
						if (pageNo == 1) {
							mAdapter.updateAppList(result);
						} else {
							mAdapter.insertAppList(result);
						}
						pageNo++;
						showNotFoundOrLoadingView(false, false);
						mListView.setVisibility(View.VISIBLE);
					}
				} else {
					if (mAdapter.getCount() < 1) {
						showNotFoundOrLoadingView(true, false);
						showNoHistory(false);
						mListView.setVisibility(View.GONE);
					} else {
						showNoHistory(false);
						showNotFoundOrLoadingView(false, false);
						mListView.setVisibility(View.VISIBLE);
					}
				}
			} catch (Exception e) {
			}
			mListView.onLoadMoreComplete();
		}
	}
	@Override
	public void onLoadMore() {
		loadPointHistory();		
	}
	@Override
	public void onStart() {
	    super.onStart();
	   // The rest of your onStart() code.
	    try {
	    	EasyTracker.getInstance(this).activityStart(this);  // Add this method.
		} catch (Exception e) {			
		}
	  }

	  @Override
	  public void onStop() {
	    super.onStop();
	    // The rest of your onStop() code.
	    try {
	    	EasyTracker.getInstance(this).activityStop(this);  // Add this method.
		} catch (Exception e) {
		}
	  }
	}
